﻿using DevExpress.Utils.Win;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Popup;
using DevExpress.XtraGrid.Editors;
using DevExpress.XtraLayout;
using DXApplicationTangche.UC.圣同润.merchant;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 广州爱特安为科技.UC.圣同润.客服
{
    public partial class Form手动发货 : DevExpress.XtraEditors.XtraForm
    {
        private UnDeliveryOrderLogisticsInfo unDeliveryOrderLogisticsInfo = new UnDeliveryOrderLogisticsInfo();
        private String brandName;
        private int brandId;
        private String deliveryName { get; set; }
        public Form手动发货(UnDeliveryOrderLogisticsInfo unDeliveryOrderLogisticsInfo, int brandId, String brandName)
        {
            InitializeComponent();
            this.unDeliveryOrderLogisticsInfo = unDeliveryOrderLogisticsInfo;
            this.brandId = brandId;
            this.brandName = brandName;
            this.showData();
            //  快递公司
            this.searchLookUpEdit物流公司.Properties.DataSource = MerchantService.getDeliveryCompanyList();
        }

        private void showData()
        {
            this.textBox订单号.Text = this.unDeliveryOrderLogisticsInfo.order_sn;
            this.textBox收件人.Text = this.unDeliveryOrderLogisticsInfo.real_name;
            this.textBox电话.Text = this.unDeliveryOrderLogisticsInfo.user_phone;
            this.textBox商品名.Text = this.unDeliveryOrderLogisticsInfo.product_name;
            this.textBox数量.Text = this.unDeliveryOrderLogisticsInfo.total_num.ToString();
            this.memoEdit地址.Text = this.unDeliveryOrderLogisticsInfo.user_address;
        }


        #region 选择物流公司
        private void searchLookUpEdit物流公司_Popup(object sender, EventArgs e)
        {
            PopupSearchLookUpEditForm form = (sender as IPopupControl).PopupWindow as PopupSearchLookUpEditForm;
            SearchEditLookUpPopup popup = form.Controls.OfType<SearchEditLookUpPopup>().FirstOrDefault();
            LayoutControl layout = popup.Controls.OfType<LayoutControl>().FirstOrDefault();
            //如果窗体内空间没有确认按钮，则自定义确认simplebutton，取消simplebutton，选中结果label
            if (layout.Controls.OfType<Control>().Where(ct => ct.Name == "btOK").FirstOrDefault() == null)
            {
                //得到空的空间
                EmptySpaceItem a = layout.Items.Where(it => it.TypeName == "EmptySpaceItem").FirstOrDefault() as EmptySpaceItem;

                //得到取消按钮，重写点击事件
                Control clearBtn = layout.Controls.OfType<Control>().Where(ct => ct.Name == "btClear").FirstOrDefault();
                LayoutControlItem clearLCI = (LayoutControlItem)layout.GetItemByControl(clearBtn);
                clearBtn.Click += clear物流公司Btn_Click;

                //添加一个simplebutton控件(确认按钮)
                LayoutControlItem myLCI = (LayoutControlItem)clearLCI.Owner.CreateLayoutItem(clearLCI.Parent);
                myLCI.TextVisible = false;
            }
        }
        private void searchLookUpEdit物流公司View_Click(object sender, EventArgs e)
        {
            int[] a = this.searchLookUpEdit物流公司.Properties.View.GetSelectedRows();
            if (a.Length > 0)
            {
                foreach (int rowHandle in a)
                {
                    this.deliveryName = this.searchLookUpEdit物流公司.Properties.View.GetRowCellValue(rowHandle, "deliveryName").ToString();
                }
            }
        }

        /// <summary>
        /// 清除按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void clear物流公司Btn_Click(object sender, EventArgs e)
        {
            this.searchLookUpEdit物流公司.ToolTip = null;
            searchLookUpEdit物流公司.EditValue = null;
        }
        private void searchLookUpEdit物流公司_CustomDisplayText(object sender, DevExpress.XtraEditors.Controls.CustomDisplayTextEventArgs e)
        {
            if (null != e.Value)
                e.DisplayText = this.deliveryName;
        }
        #endregion

        private void simpleButton发货_Click(object sender, EventArgs e)
        {
            if (String.IsNullOrWhiteSpace(this.textBox物流单号.Text) || String.IsNullOrEmpty(this.deliveryName))
            {
                MessageBox.Show("请将物流信息填写完整");
                return;
            }
            List<String> logisticsNos = new List<String>();
            logisticsNos.Add(this.unDeliveryOrderLogisticsInfo.order_sn);
            //  发货
            MerchantService.saveVirtualLogisticsBodies(
                this.brandId,
                this.brandName,
                this.deliveryName,
                this.textBox物流单号.Text.Trim(),
                logisticsNos
                );
            this.Close();
        }
    }
}